package com.hilaver.dzmis.service;

import java.awt.Color;
import java.util.Locale;

import com.hilaver.dzmis.service.impl.I18nServiceImpl;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.FontSelector;
import com.lowagie.text.pdf.PdfPCell;

public class BasePDFService {
	
	public static float CELL_HEIGHT = 16f;
	
	protected Locale locale;
	protected I18nServiceImpl i18n;
	protected FontSelector selector;
	protected FontSelector boldSelector;
	protected String realFileDir;
	
	protected PdfPCell getTableCell(String key, Boolean hasBorder, Integer colSpan)
			throws Exception {
		if(key == null){
			key = "";
		}
		String message = this.i18n.getShortMessage(locale, key);
		if ("".equals(message)) {
			message = key;
		}
		PdfPCell cell = new PdfPCell(new Paragraph(this.selector
				.process(message)));
		cell.setColspan(colSpan);
		cell.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
		cell.setVerticalAlignment(Paragraph.ALIGN_CENTER);
		cell.setMinimumHeight(CELL_HEIGHT);
		if (!hasBorder) {
			cell.setBorder(Rectangle.NO_BORDER);
		}
		return cell;
	}
	
	protected PdfPCell getTableCell(String key, Integer colSpan, Integer border)
	throws Exception {
		if(key == null){
			key = "";
		}
		String message = this.i18n.getShortMessage(locale, key);
		if ("".equals(message)) {
			message = key;
		}
		PdfPCell cell = new PdfPCell(new Paragraph(this.selector
				.process(message)));
		cell.setColspan(colSpan);
		cell.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
		cell.setVerticalAlignment(Paragraph.ALIGN_CENTER);
		cell.setMinimumHeight(CELL_HEIGHT);
		cell.setBorder(border);
		return cell;
	}
	
	protected PdfPCell getTableCellBold(String key, Boolean hasBorder, Integer colSpan)
		throws Exception {
		if(key == null){
			key = "";
		}
		String message = this.i18n.getShortMessage(locale, key);
		if ("".equals(message)) {
			message = key;
		}
		PdfPCell cell = new PdfPCell(new Paragraph(this.boldSelector
				.process(message)));
		cell.setColspan(colSpan);
		cell.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
		cell.setVerticalAlignment(Paragraph.ALIGN_CENTER);
		cell.setMinimumHeight(CELL_HEIGHT);
		if (!hasBorder) {
			cell.setBorder(Rectangle.NO_BORDER);
		}
		return cell;
	}
	
	protected PdfPCell getTableCellBoldBGGrey(String key, Boolean hasBorder, Integer colSpan)
	throws Exception {
		if(key == null){
			key = "";
		}
		String message = this.i18n.getShortMessage(locale, key);
		if ("".equals(message)) {
			message = key;
		}
		PdfPCell cell = new PdfPCell(new Paragraph(this.boldSelector
				.process(message)));
		cell.setColspan(colSpan);
		cell.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
		cell.setVerticalAlignment(Paragraph.ALIGN_CENTER);
		cell.setMinimumHeight(CELL_HEIGHT);
		cell.setBackgroundColor(Color.LIGHT_GRAY);
		if (!hasBorder) {
			cell.setBorder(Rectangle.NO_BORDER);
		}
		return cell;
	}
	
	protected PdfPCell getTableCellBold(String key, Integer colSpan, Integer border)
		throws Exception {
		if(key == null){
			key = "";
		}
		String message = this.i18n.getShortMessage(locale, key);
		if ("".equals(message)) {
			message = key;
		}
		PdfPCell cell = new PdfPCell(new Paragraph(this.boldSelector
				.process(message)));
		cell.setColspan(colSpan);
		cell.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
		cell.setVerticalAlignment(Paragraph.ALIGN_CENTER);
		cell.setMinimumHeight(CELL_HEIGHT);
		cell.setBorder(border);
		return cell;
	}
	
	protected PdfPCell getTableCellBold(String key, Boolean hasBorder, Integer colSpan, Integer align)
	throws Exception {
		if(key == null){
			key = "";
		}
		String message = this.i18n.getShortMessage(locale, key);
		if ("".equals(message)) {
			message = key;
		}
		PdfPCell cell = new PdfPCell(new Paragraph(this.boldSelector
				.process(message)));
		cell.setColspan(colSpan);
		cell.setHorizontalAlignment(align);
		cell.setVerticalAlignment(Paragraph.ALIGN_CENTER);
		cell.setMinimumHeight(CELL_HEIGHT);
		if (!hasBorder) {
			cell.setBorder(Rectangle.NO_BORDER);
		}
		return cell;
	}

	protected PdfPCell getTableCell(String key, Boolean hasBorder,
			Integer colSpan, Integer align) throws Exception {
		String message = this.i18n.getShortMessage(locale, key);
		if ("".equals(message)) {
			message = key;
		}
		PdfPCell cell = new PdfPCell(new Paragraph(this.selector
				.process(message)));
		cell.setColspan(colSpan);
		cell.setHorizontalAlignment(align);
		cell.setVerticalAlignment(Paragraph.ALIGN_CENTER);
		cell.setPaddingRight(10);
		cell.setMinimumHeight(CELL_HEIGHT);
		if (!hasBorder) {
			cell.setBorder(Rectangle.NO_BORDER);
		}
		return cell;
	}

	protected PdfPCell getTableCell(String[] key, Boolean hasBorder,
			Integer colSpan) throws Exception {
		String message = "";
		for (int i = 0; i < key.length; i++) {
			String tmp = this.i18n.getShortMessage(locale, key[i]);
			if ("".equals(tmp)) {
				tmp = key[i];
			}
			message += tmp + " ";
		}

		PdfPCell cell = new PdfPCell(new Paragraph(this.selector
				.process(message)));
		cell.setColspan(colSpan);
		cell.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
		cell.setVerticalAlignment(Paragraph.ALIGN_CENTER);
		cell.setMinimumHeight(CELL_HEIGHT);
		if (!hasBorder) {
			cell.setBorder(Rectangle.NO_BORDER);
		}
		return cell;
	}
	
	protected PdfPCell getTableCell(String[] key, Integer border,
			Integer colSpan) throws Exception {
		String message = "";
		for (int i = 0; i < key.length; i++) {
			String tmp = this.i18n.getShortMessage(locale, key[i]);
			if ("".equals(tmp)) {
				tmp = key[i];
			}
			message += tmp + " ";
		}

		PdfPCell cell = new PdfPCell(new Paragraph(this.selector
				.process(message)));
		cell.setColspan(colSpan);
		cell.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
		cell.setVerticalAlignment(Paragraph.ALIGN_CENTER);
		cell.setMinimumHeight(CELL_HEIGHT);
		cell.setBorder(border);
		return cell;
	}
	
	protected PdfPCell getTableCellBold(String[] key, Boolean hasBorder,
			Integer colSpan) throws Exception {
		String message = "";
		for (int i = 0; i < key.length; i++) {
			String tmp = this.i18n.getShortMessage(locale, key[i]);
			if ("".equals(tmp)) {
				tmp = key[i];
			}
			message += tmp + " ";
		}

		PdfPCell cell = new PdfPCell(new Paragraph(this.boldSelector
				.process(message)));
		cell.setColspan(colSpan);
		cell.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
		cell.setVerticalAlignment(Paragraph.ALIGN_CENTER);
		cell.setMinimumHeight(CELL_HEIGHT);
		if (!hasBorder) {
			cell.setBorder(Rectangle.NO_BORDER);
		}
		return cell;
	}
	
	protected PdfPCell getTableCellBold(String[] key, Integer border,
			Integer colSpan) throws Exception {
		String message = "";
		for (int i = 0; i < key.length; i++) {
			String tmp = this.i18n.getShortMessage(locale, key[i]);
			if ("".equals(tmp)) {
				tmp = key[i];
			}
			message += tmp + " ";
		}

		PdfPCell cell = new PdfPCell(new Paragraph(this.boldSelector
				.process(message)));
		cell.setColspan(colSpan);
		cell.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
		cell.setVerticalAlignment(Paragraph.ALIGN_CENTER);
		cell.setMinimumHeight(CELL_HEIGHT);
		cell.setBorder(border);
		return cell;
	}

	protected PdfPCell getTableCell(String[] key, Boolean hasBorder,
			Integer colSpan, Integer align) throws Exception {
		String message = "";
		for (int i = 0; i < key.length; i++) {
			String tmp = this.i18n.getShortMessage(locale, key[i]);
			if ("".equals(tmp)) {
				tmp = key[i];
			}
			message += tmp + " ";
		}

		PdfPCell cell = new PdfPCell(new Paragraph(this.selector
				.process(message)));
		cell.setColspan(colSpan);
		cell.setHorizontalAlignment(align);
		cell.setVerticalAlignment(Paragraph.ALIGN_CENTER);
		cell.setMinimumHeight(CELL_HEIGHT);
		if (!hasBorder) {
			cell.setBorder(Rectangle.NO_BORDER);
		}
		return cell;
	}
	
	protected PdfPCell getTableCellBold(String[] key, Boolean hasBorder,
			Integer colSpan, Integer align) throws Exception {
		String message = "";
		for (int i = 0; i < key.length; i++) {
			String tmp = this.i18n.getShortMessage(locale, key[i]);
			if ("".equals(tmp)) {
				tmp = key[i];
			}
			message += tmp + " ";
		}

		PdfPCell cell = new PdfPCell(new Paragraph(this.boldSelector
				.process(message)));
		cell.setColspan(colSpan);
		cell.setHorizontalAlignment(align);
		cell.setVerticalAlignment(Paragraph.ALIGN_CENTER);
		cell.setMinimumHeight(CELL_HEIGHT);
		if (!hasBorder) {
			cell.setBorder(Rectangle.NO_BORDER);
		}
		return cell;
	}

	protected PdfPCell getEmptyLine(Integer colSpan) {
		PdfPCell cell = new PdfPCell(new Paragraph(""));
		cell.setColspan(colSpan);
		cell.setBorder(Rectangle.NO_BORDER);
		cell.setFixedHeight(2);
		return cell;
	}
	
	protected PdfPCell getEmptyLine(Integer colSpan, Integer border) {
		PdfPCell cell = new PdfPCell(new Paragraph(""));
		cell.setColspan(colSpan);
		cell.setBorder(border);
		cell.setFixedHeight(2);
		return cell;
	}
	
	public void setRealFileDir(String realFileDir) {
		this.realFileDir = realFileDir;
	}
}
